﻿using System;
using System.Windows;
using System.Windows.Media;

namespace ine.Extensions
{
    public static class WindowExtensions
    {
        public static void ForWindow(this object instance, Action<Window> action)
        {
            Window window = GetTopLevelControl((DependencyObject)instance);

            if (window != null)
            {
                action.Invoke(window);
            }
        }

        private static Window GetTopLevelControl(DependencyObject control)
        {
            DependencyObject tmp = control;
            DependencyObject parent = control;

            while ((tmp = VisualTreeHelper.GetParent(tmp)) != null)
            {
                parent = tmp;
            }

            return (Window)parent;
        }
    }
}
